#include "scanner.h"
//#define YY_NO_UNISTD_H
#include "lex.yy.cc"
#include "symtab.h"
#include "sourceline.h"


Scanner::Scanner()
{
	lexer = new yyFlexLexer();
	m_sourceLine = new SourceLine(200);
}

Scanner::~Scanner()
{
}

void Scanner::setCurrentToken(TokenCode tc, DataType ty, OpType oper) // sets the corresponding fields in m_currentToken
{
	m_currentToken.setTokenCode(tc);
	m_currentToken.setDataType(ty);
	//if(m_currentToken.getTokenCode()==tc_ID || m_currentToken.getTokenCode()==tc_NUMBER)
	//HKH:  Breytti fallinu þannig að við köllum ekki í m_currentToken.getTokenCode() heldur
	//		notum tc, þar sem það er hvort eð er breytan sem við notum í setTokenCode  tveimur
	//		línum ofar.
	if( tc == tc_ID || tc == tc_NUMBER )
	{
		m_currentToken.setDataValue(lexer->YYText());
		//printf ("%s \n", (char*)"setCurrentToken:" );	
		//printf ("%s \n", m_currentToken.getDataValue().lexeme );
	} else
	{
		m_currentToken.setDataValue(oper);	
		
	}
	DataValue temp = m_currentToken.getDataValue();  //Todo: Hvað er þetta að gera?
}

//void Scanner::setCurrentToken(TokenCode tc, DataType ty, const char* lexeme) // sets the corresponding fields in m_currentToken
//{
//	m_currentToken.setTokenCode(tc);
//	m_currentToken.setDataType(ty);
//	m_currentToken.setDataValue(lexeme);
//}

TokenCode Scanner::keywordCheck(const char *str)
{
	//check if *str is a keyword, else return tc_ID

	keyWord kw;

    return kw.tCode;
	
}

Token* Scanner::nextToken(void)
{
	//returns a pointer to the next token found
	//This function uses its FlexLexer instance tCode = (TokenCode)lexer->yylex();
	
	TokenCode tCode = (TokenCode)lexer->yylex();

	while( tCode == tc_NEWLINE || tCode == tc_SPACE || tCode == tc_TAB || tCode == tc_COMMENT  )
	{
		if( tCode == tc_NEWLINE || tCode == tc_COMMENT )
		{
			m_sourceLine->printLine();
			m_sourceLine->clearLine();
			m_sourceLine->newLine();
		}

		if( tCode == tc_SPACE )
		{
			m_sourceLine->buildLine(" ");
		}

		if( tCode == tc_TAB )
		{
			m_sourceLine->buildLine("    ");
		}
		tCode = (TokenCode)lexer->yylex();
	}

	m_sourceLine->buildLine(lexer->YYText());
	setCurrentToken(tCode, Type, Oper);
	return &m_currentToken;



	//Token* tempToken;
	//tempToken->g
	//lexer->tCode = (TokenCode)lexer->yylex(); 
}

void Scanner::setCurrentError(char * msg)
{
	m_sourceLine->setError(msg);
}

void Scanner::clearCurrentError()
{
	m_sourceLine->clearError();
}

